package com.weblog.user.controller;

import com.weblog.model.user.Permission;
import com.weblog.model.user.Role;
import com.weblog.user.service.RoleService;
import entity.Result;
import entity.ResultMessage;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author MaoLin Wang
 * @date 2019/9/1817:59
 */
@RestController
@CrossOrigin
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 角色列表
     * @return
     */
    @GetMapping
    public Result RoleList(){
        return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,roleService.RoleList());
    }

    /**
     * 添加角色
     * @param role
     * @return
     */
    @PostMapping
    public Result addRole(@RequestBody Role role){
        roleService.addRole(role);
        return new Result(true, StatusCode.OK, ResultMessage.SAVE_SUCCESS);
    }

    /**
     * 根据角色id查询角色
     * @param roleid
     * @return
     */
    @GetMapping("/{roleid}")
    public Result findRoleById(@PathVariable("roleid") String roleid){
        Role role= roleService.findRoleById(roleid);
        return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,role);
    }

    /**
     * 根据角色id查询权限列表
     * @param rid
     * @return
     */
    @GetMapping("/permission/{rid}")
    public Result findPermissionsByRoleId (@PathVariable("rid")String rid){
        List<Permission>permissions=roleService.findPermissionsByRoleId(rid);
        return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,permissions);
    }

    /**
     * 修改角色
     * @param role
     * @param rid
     * @return
     */
    @PutMapping("/{rid}")
    public Result updateRole(@RequestBody Role role, @PathVariable("rid") String rid){
            role.setRid(rid);
         int flag=roleService.update(role);
         if(flag==1){
             return new Result(true, StatusCode.OK, ResultMessage.UPDATE_SUCCESS);
         }
        return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);

    }
    /**
     * 弃用角色
     * @param rid
     * @return
     */
    @PutMapping("/qiyong/{rid}")
    public Result roleNotUse(@PathVariable("rid") String rid){

        int flag=roleService.roleNotUse(rid);
        if(flag==1){
            return new Result(true, StatusCode.OK, ResultMessage.UPDATE_SUCCESS);
        }
        return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);

    }


    /**
     * 给角色添加管理员
     * @param rid 角色id
     * @param id 管理员id
     * @return
     */
    @PostMapping("/admin/{rid}/{id}")
    public Result addUserToRole(@PathVariable("rid")String rid, @PathVariable("id")String id){
        int flag= roleService.addAdminToRole(rid,id);
        if(flag==1){
            return new Result(true, StatusCode.OK, ResultMessage.UPDATE_SUCCESS);
        }else if(flag == 0){
            //重复添加
            return new Result(false, StatusCode.REPERROR, ResultMessage.DATA_REPETED);
        }
        //权限不足
        return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
    }


    /**
     * 给角色添加权限
     * @param rid 角色id
     * @param pid 权限id
     * @return
     */
    @PostMapping("/permission/{rid}/{pid}")
    public Result addPermissionToRole(@PathVariable("rid")String rid, @PathVariable("pid")String pid){
        int flag= roleService.addPermissionToRole(rid,pid);
        if(flag==1){
            return new Result(true, StatusCode.OK, ResultMessage.UPDATE_SUCCESS);
        }else if(flag == 0){
            //重复添加
            return new Result(false, StatusCode.REPERROR, ResultMessage.DATA_REPETED);
        }else if(flag == 3){
            return new Result(false, StatusCode.NOT_ALLOWED, ResultMessage.PERMISSION_NOT_ALLOWED);
        }
        //权限不足
        return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
    }

    /**
     * 根据角色id和权限id删除角色权限
     * @param rid
     * @param pid
     * @return
     */
    @DeleteMapping("/permission/{rid}/{pid}")
    public Result deletePermissionOfRole(@PathVariable("rid")String rid, @PathVariable("pid")String pid){
        int flag= roleService.deletePermissionOfRole(rid,pid);
        if(flag==1){
            return new Result(true, StatusCode.OK,"取消成功");
        }
        //权限不足
        return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
    }
}
